<?php

namespace App\Admin\Controllers;

use App\Admin\Forms\WangEditor;
use App\Admin\Renderables\ChatContent;
use App\Http\Controllers\Controller;
use App\Models\ChatContentModel;
use App\Models\UsersManagementModel;
use App\Models\WorkOrderModel;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Http\JsonResponse;
use Dcat\Admin\Layout\Column;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;

class CustomerDockingController extends Controller
{
    protected function index(Content $content)
    {
        Admin::css('/css/components/customer-docking-form.css');
        return $content
            ->header(admin_trans_label('customer_docking'))
            ->description(admin_trans_label('customer_docking'))
            ->body(function (Row $row) {
                $row->column(6, function (Column $column) {
                    $column->row(new ChatContent(1, 2));
                    $column->row(new WangEditor());
                });

                $row->column(3, function (Column $column) {
                    $column->row($this->workOrder(1));
                });

                $row->column(3, function (Column $column) {
                    $column->row($this->usersManagement(2));
                });
            });
    }

    private function workOrder($workId)
    {
        return Form::make(new WorkOrderModel(), function (Form $form) use ($workId) {
            $form->action('/work_order/' . $workId);
            $form->title('工单信息');
            $this->init($form);
            $form->text('title', '工单标题');
            $form->select('classification', '工单分类')->options([1 => '退换货'])->required();
            $form->select('priority', '工单优先级')->options([1 => '一般'])->required();
            $form->select('state', '工单状态')->options([1 => '未处理', 2 => '处理中'])->required();
            $form->date('deadline', '截止时间');
            $form->text('phone_model', '手机型号');
            $form->select('broadcast_name', '广播名')->options([0 => 'ASD Pro']);
            $form->select('firmware', '固件')->options([0 => 'ASD']);
            $form->textarea('remarks', '备注');
            $form->display('created_at', '创建时间');
        })->edit($workId);
    }

    private function usersManagement($userId)
    {
        return Form::make(new UsersManagementModel(), function (Form $form) use ($userId) {
            $form->action('/users_management/' . $userId);
            $form->title('访客信息');
            $this->init($form);
            $form->text('user_name', '用户姓名')->required();
            $form->email('user_email', '邮箱')->required();
            $form->text('phone_number', '手机');
            $form->text('user_address', '地址');
            $form->textarea('remarks', '备注');
        })->edit($userId);
    }

    private function init(Form $form)
    {
        $form->tools(function (Form\Tools $tools) {
            $tools->disableList();
            // 去掉跳转详情页按钮
            $tools->disableView();
            // 去掉删除按钮
            $tools->disableDelete();

        });

        $form->footer(function (Form\Footer $footer) {
            $footer->view('admin::form.footer');
        });
        $form->disableResetButton();
        // 去掉`查看`checkbox
        $form->disableViewCheck();
        // 去掉`继续编辑`checkbox
        $form->disableEditingCheck();
        // 去掉`继续创建`checkbox
        $form->disableCreatingCheck();
    }

    protected function getData()
    {
        return response(ChatContentModel::all());
    }

    protected function workOrderEdit(Request $request, $id)
    {
        $data = $request->all();
        $workOrderModel = WorkOrderModel::find($id);
        $keys = $workOrderModel->getAttributes();
        foreach ($keys as $key => $value) {
            if (array_key_exists($key, $data)) {
                $workOrderModel[$key] = $data[$key];
            }
        }
        $isSave = $workOrderModel->save();
        if ($isSave) {
            return JsonResponse::make()->success('修改成功');
        }

        return JsonResponse::make()->success('修改失败');
    }

    protected function usersManagementEdit(Request $request, $id)
    {
        $data = $request->all();
        $usersManagementModel = UsersManagementModel::find($id);
        $keys = $usersManagementModel->getAttributes();
        foreach ($keys as $key => $value) {
            if (array_key_exists($key, $data)) {
                $usersManagementModel[$key] = $data[$key];
            }
        }
        $isSave = $usersManagementModel->save();
        if ($isSave) {
            return JsonResponse::make()->success('修改成功');
        }

        return JsonResponse::make()->success('修改失败');
    }
}
